<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/' }">
        <i class="el-icon-s-promotion"></i> 后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item>添加管理员</el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>

    <el-form :model="picture" :rules="rules" ref="ruleForm" label-width="80px" class="demo-ruleForm">
      <el-form-item label="图片" prop="picture">
        <el-upload
            class="avatar-uploader"
            :headers="jwtObj"
            action="http://localhost:8000/upload"
            name="picture"
            :show-file-list="false"
            :on-success="handleAvatarSuccess">
          <img v-if="imageUrl" :src="imageUrl" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="房屋id" prop="roomId">
        <el-input v-model="picture.roomId" placeholder="请输入房屋id"></el-input>
      </el-form-item>


      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">立即添加</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import axios from "axios";
import CommonUtils from "@/views/utils/CommonUtils";

export default {
  name: "addAdmin",
  data() {
    return {
      jwtObj: {Authorization: localStorage.getItem('jwt')},
      imageUrl: '',
      roleArr: [],
      picture: {
        url:'',
        roomId:'',
        roleIds: []
      },
      rules: {
        roomId: [
          { required: true, message: '请输入房屋Id', trigger: 'blur' },
          { min: 1, max: 100000, message: '长度在 1 到 100000 个字符', trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.admin.roleIds.length===0){
            this.$message.warning('必须选择角色信息');
            return;
          }
          let formData = this.qs.stringify(this.admin);
          axios
              .create(this.getJwt())
              .post("http://localhost:8000/pictures/add-new",formData).then((res)=> {
                let data = res.data;
                if (data.state === 20000){
                  this.$message({
                    dangerouslyUseHTMLString: true,
                    message: "恭喜您，添加成功",
                    type: 'success'
                  });
                  this.resetForm(formName);
                  return;
                }
                CommonUtils.methods.isFail(data,this);
              })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    handleAvatarSuccess(res,file, fileList) {
      this.imageUrl = URL.createObjectURL(file.raw);
      if (res.state === 20000){
        this.admin.avatar = res.data;
      }else {
        this.$message({
          dangerouslyUseHTMLString: true,
          message: "<strong>"+res.message+"</strong>",
          type: 'error'
        });
      }
    },
    getJwt(){
      return {'headers':{'Authorization': localStorage.getItem('jwt')}};
    }
  },
  mounted() {
    axios
        .create(this.getJwt())
        .get("http://localhost:8000/roles/list").then((res)=>{
          let data = res.data;
          if (data.state === 20000){
            this.roleArr = data.data;
            return;
          }
          CommonUtils.methods.isFail(data,this);
    })
  }
}
</script>

<style>

  h3{
    color: indianred;
  }
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
  .el-upload, .el-select, .el-switch {
    float: left;
  }
  .el-form{
    width: 800px;
  }
</style>